package com.xunqi.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import javax.jms.*;
import java.io.IOException;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-06-22 09:54
 **/
public class JmsConsumer_Topic {

    public static final String ACTIVEMQ_URL = "tcp://192.168.77.130:61616";
    public static final String TOPIC_NAME = "topic01-xunqi";

    @Test
    public void consumerTopic() throws JMSException, IOException {

        System.out.println("我是1号消费者");

        //1、创建连接工厂,按照给定的url地址，采用默认的用户名和密码
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        //2、通过连接工厂，获得Collection并启动访问
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        //3、创建会话session
        //两个参数transacted=事务,acknowledgeMode=确认模式(签收)
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4、创建目的地（具体是队列还是主题topic）
        Topic topic = session.createTopic(TOPIC_NAME);

        //5.创建消息的消费者,指定消费哪一个队列里面的消息
        MessageConsumer messageConsumer = session.createConsumer(topic);

        /*
        同步阻塞
        while (true) {
            TextMessage textMessage = (TextMessage)messageConsumer.receive(4000L);

            if (null != textMessage) {
                System.out.println("*********消费者接受到消息：" + textMessage.getText());
            } else {
                break;
            }
        }

        //关闭资源
        messageConsumer.close();
        session.close();
        connection.close();*/


        /**
         *         异步非阻塞式方式监听器(onMessage)
         *         订阅者或消费者通过创建的消费者对象,给消费者注册消息监听器setMessageListener,
         *         当消息有消息的时候,系统会自动调用MessageListener类的onMessage方法
         *         我们只需要在onMessage方法内判断消息类型即可获取消息
         */
/*        messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if (null != message && message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    try {
                        System.out.println("*********消费者接受到消息：" + textMessage.getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });*/

        messageConsumer.setMessageListener((message -> {
            if (null != message && message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("*********消费者接受到Topic消息：" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }));
        System.in.read();
        messageConsumer.close();
        session.close();
        connection.close();


    }

}
